package com.eva.androidlibrary.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.eva.androidlibrary.R;


/**
 * 获取验证码控件
 *
 * @author eva
 * @since 15/10/24
 */
public class TimerWidget extends TextView implements View.OnClickListener{

    private static final int DEFAULT_TIME     = 60;
    private static final int DEFAULT_INTERVAL = 1000;

    private int mActiveColor = 0xFFFFFFFF;
    private int mNonActiveColor;
    private OnTimerListener  onTimerListener;

    private int mTotalTime;
    private int mInterval;

    private CountDownTimer mTimer;

    public interface OnTimerListener {
        void onClick();

        void onTiming(int remainTime);

        void onFinish();
    }


    public TimerWidget(Context context) {
        super(context);
        init(context, null, 0, 0);
    }

    public TimerWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0, 0);

    }

    public TimerWidget(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, 0);

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TimerWidget(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr, defStyleRes);
    }

    public void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        setText(getResources().getString(R.string.get_auth_code));
        setOnClickListener(this);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TimerWidget, defStyleAttr, defStyleRes);
        mActiveColor = a.getColor(R.styleable.TimerWidget_activeColor, mActiveColor);
        mNonActiveColor = a.getColor(R.styleable.TimerWidget_nonActiveColor, getResources().getColor(R.color.sliver_grey));
        mTotalTime = a.getInteger(R.styleable.TimerWidget_totalTime, DEFAULT_TIME);
        mInterval = a.getInteger(R.styleable.TimerWidget_interval, DEFAULT_INTERVAL);
        a.recycle();

        setTextColor(mActiveColor);
    }

    /**
     * 设置可点击状态字体颜色
     *
     * @param color
     */
    public void setActiveColor(int color) {
        this.mActiveColor = color;
    }

    /**
     * 设置非点击状态字体颜色
     *
     * @param color
     */
    public void setNonActiveColor(int color) {
        this.mNonActiveColor = color;
    }

    public void setOnTimerListener(OnTimerListener listener) {
        this.onTimerListener = listener;
    }

    /**
     * 设置间隔时间
     *
     * @param interval
     */
    public void setInterval(int interval) {
        this.mInterval = interval;
    }

    /**
     * 设置倒计时总时长，单位：秒
     *
     * @param time
     */
    public void setTotalTime(int time) {
        this.mTotalTime = time;
    }

    @Override
    public void onClick(View v) {
        if (this.isEnabled()) {
            this.setEnabled(false);
            this.setTextColor(mNonActiveColor);
            startTimer();
            if (onTimerListener != null)
                onTimerListener.onClick();
        }
    }

    /**
     * 开始计时
     */
    private void startTimer() {
        if (mTimer == null) {
            mTimer = new CountDownTimer(mTotalTime * 1000, mInterval) {
                @Override
                public void onTick(long millisUntilFinished) {
                    int remainTime = (int) (millisUntilFinished / 1000);
                    setText(getResources().getString(R.string.getting_auth_code, remainTime));
                    if (onTimerListener != null)
                        onTimerListener.onTiming(remainTime);
                }

                @Override
                public void onFinish() {
                    reset();
                }
            };
        }
        mTimer.start();
    }

    public void reset() {
        setEnabled(true);
        setText(getResources().getString(R.string.get_auth_code));
        setTextColor(mActiveColor);
        mTimer.cancel();
        mTimer.onFinish();
    }
}
